package io.renren.common.utils;

//import io.renren.common.sms.CheckCodeUtils;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.SimpleEmail;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author: Mr.Wang
 * @description:
 * @create: $ $
 **/
public class EmailUtils {
    private static final String from = "jmqczj@qd.shandong.cn";
    private static final String fromName = "青岛市即墨区财政局";
    private static final String charSet = "utf-8";
    private static final String username = "jmqczj@qd.shandong.cn";
    private static final String password = "jmcz1234";
    private static Map<String, String> hostMap = new HashMap<String, String>();

    static {
        // 126
        //hostMap.put("smtp.126", "smtp.126.com");
        // qq
        //hostMap.put("smtp.qq", "smtp.qq.com");
        // 163
        //hostMap.put("smtp.163", "smtp.163.com");
        // sina
        //hostMap.put("smtp.sina", "smtp.sina.com.cn");
        // tom
        //hostMap.put("smtp.tom", "smtp.tom.com");
        // 263
        //hostMap.put("smtp.263", "smtp.263.net");
        // yahoo
        //hostMap.put("smtp.yahoo", "smtp.mail.yahoo.com");
        // hotmail
        //hostMap.put("smtp.hotmail", "smtp.live.com");
        // gmail
        //hostMap.put("smtp.gmail", "smtp.gmail.com");
        //hostMap.put("smtp.port.gmail", "465");
    }

    public static String getHost(String email) throws Exception {
        Pattern pattern = Pattern.compile("\\w+@(\\w+)(\\.\\w+){1,2}");
        Matcher matcher = pattern.matcher(email);
        String key = "unSupportEmail";
        if (matcher.find()) {
            key = "smtp." + matcher.group(1);
        }
        if (hostMap.containsKey(key)) {
            return hostMap.get(key);
        } else {
            throw new Exception("unSupportEmail");
        }
    }

    public static int getSmtpPort(String email) throws Exception {
        Pattern pattern = Pattern.compile("\\w+@(\\w+)(\\.\\w+){1,2}");
        Matcher matcher = pattern.matcher(email);
        String key = "unSupportEmail";
        if (matcher.find()) {
            key = "smtp.port." + matcher.group(1);
        }
        if (hostMap.containsKey(key)) {
            return Integer.parseInt(hostMap.get(key));
        } else {
            return 25;
        }
    }

    public static void sendSimpleTextEmail(String toMailAddr, String subject,
                                           String message, String sendName) throws EmailException {
        HtmlEmail email = new HtmlEmail();
        try {
            email.setHostName("mail.shandong.cn");
            email.setSmtpPort(587);
            email.setCharset(charSet);
            email.setAuthentication(username, password);
            email.setSSLOnConnect(true);
            email.setFrom(from, sendName, charSet);
            email.setSubject(subject);
            email.setHtmlMsg(message);
            email.addTo(toMailAddr);
            email.send();
            System.out.println("email send true!");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("email send error!");
        }
    }


//    public static String sendMailCode(String toMail){
//        try {
//            //String code = CheckCodeUtils.random() + "";
//            sendSimpleTextEmail(toMail,"Confirmation of Visitor Pre-registration(AD)","Your Security code is "+code,"");
//            return code;
//        } catch (EmailException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }



    public static void main(String[] args) {
        // HtmlEmail hemail = new HtmlEmail();
        // try {
        // hemail.setHostName("smtp.exmail.qq.com");
        // hemail.setCharset("utf-8");
        // hemail.addTo("fly.1206@qq.com");
        // hemail.setFrom("ddd@ddd.com", "xxx");
        // hemail.setAuthentication("ddg@ddd.com", "dd@aa");
        // hemail.setSubject("sendemail test!");
        // hemail.setMsg("<a href=\"http://www.google.cn\">baidu</a><br/>");
        // hemail.send();
        // System.out.println("email send true!");
        // } catch (Exception e) {
        // e.printStackTrace();
        // System.out.println("email send error!");
        // }
        try {
            EmailUtils.sendSimpleTextEmail("hu_cheng86@126.com","Confirmation of Visitor Pre-registration(AD)","Your Security code is ","");
        } catch (EmailException e) {
            e.printStackTrace();
        }
    }

}
